var db = require('../dbutil/index');
var sql = require('../dbutil/sql');


module.exports={
    goodsByCate:(req,res)=>{
        let cid=req.body.cid;  //请求体获取
        db.query(sql.goods.goodsByCate,[cid],(err,results)=>{ //提供两个参数值cid来替换sql.js里goodsByCate方法中的两个？占位符 （也就是多少个？就被多少个cid或其他的参数值替换）
            if (err) {
                res.send({ code: 500, message: 'sql执行错误' });
            } else {
                res.send({ code: 200, message: 'ok', data:results });
            }
        })
    },
    nocleargoods: (req, res) => {
        let keyword = req.body.keyword;  // 前端发送的是名为'keyword'的字段
        let likePattern = `%${keyword}%`; //插入到模板字符串,其中keyword是从前端传入的搜索词。“？”
        db.query(sql.goods.nocleargoods, [likePattern], (err, results) => {
            if (err) {
                res.send({ code: 500, message: 'sql执行错误' });
            } else {
                res.send({ code: 200, message: 'ok', data: results });
            }
        });
    },
    goodsById:(req,res)=>{
        let gid=req.body.gid; //从请求体中获取商品gid
        db.query(sql.goods.goodsById,[gid],(err,results)=>{ 
            if (err) {
                res.send({ code: 500, message: 'sql执行错误' });
            } else {
                res.send({ code: 200, message: 'ok', data:results[0] });
            }
        })
    },
    brands:(req,res)=>{
        db.query(sql.goods.brands, (err, results) => {
            if (err) {
                res.send({ code: 500, message: 'sql执行错误' });
            } else {
                res.send({ code: 200, message: 'ok', data: results});
            }
        })
    },
    brandsdetail: (req, res) => {
        const { bid } = req.body;
        db.query(`SELECT * FROM brands WHERE bid = ?`, [bid], (err, results) => {
          if (err) {
            res.send({ code: 500, message: 'sql执行错误' });
          } else {
            res.send({ code: 200, message: 'ok', data: results[0] }); //取数组第一个
          }
        });
      },
}